#!/bin/bash

source head.sh

echo '
    <h1>学生列表</h1>
    <table>
      <tr>
        <th>学号</th>
        <th>班级</th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
'
sql="select jx_stu.id, jx_class.name, jx_stu.name, jx_stu.age  from jx_stu  join jx_class on jx_stu.classId=jx_class.id;"

# 去缓存里获取sql变量为键的键值对的值，存放到stuList
stuList=$(redis-cli -c -h 10.10.10.27 get "$sql")

# 如果获取不到，去数据库里拿，并且把结果放到缓存里
if [ -z "$stuList" ]; then
  stuList=$($mysqllogin "$sql" | grep -v id | sed "s/\t/@/g")
  # 1@1@张三@18
  redis-cli -c -h 10.10.10.27 set "$sql" "$stuList"
fi

for stu in ${stuList[@]}; do
  # 获取用户ID
  id=$(echo $stu | awk -F "@" '{print $1}')
  classId=$(echo $stu | awk -F "@" '{print $2}')
  name=$(echo $stu | awk -F "@" '{print $3}')
  age=$(echo $stu | awk -F "@" '{print $4}')
  echo "
        <tr>
          <td>$id</td>
          <td>$classId</td>
          <td>$name</td>
          <td>$age</td>
        </tr>
  "
done

echo '
    </table>
  </div>
</body>
</html>
'

